package org.scirp.ijmis.service.review.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.scirp.ijmis.dao.review.IReleasedCommentDAO;
import org.scirp.ijmis.dao.review.IReviewPaperDAO;
import org.scirp.ijmis.po.review.ReleasedComment;
import org.scirp.ijmis.po.review.ReviewPaper;
import org.scirp.ijmis.service.review.IReviewPaperService;
import org.scirp.ijmis.util.Pagination;

public class ReviewPaperService implements IReviewPaperService {
	private static final Log log = LogFactory.getLog(ReviewPaperService.class);

	private IReviewPaperDAO reviewPaperDAO;
	private IReleasedCommentDAO releasedCommentDAO;

	public ReviewPaper selectById(Long id) {
		return reviewPaperDAO.selectById(id);
	}

	public boolean updateReviewPaper(ReviewPaper reviewPaper) {
		return reviewPaperDAO.updateReviewPaper(reviewPaper);
	}

	public ReviewPaper selectByPaperIdAndPersonId(Long paperId, Long personId) {
		return reviewPaperDAO.selectByPaperIdAndPersonId(paperId, personId);
	}

	public List<ReviewPaper> selectByPersonId(Long personId) {
		return reviewPaperDAO.selectByPersonId(personId);
	}

	public List<ReviewPaper> selectByJournalId(String isAccepted, Long journalId, Pagination pagination) {
		return reviewPaperDAO.selectByJournalId(isAccepted, journalId, pagination);
	}

	public Long selectCountByJournalIdAndIsAccepted(Long journalId, String isAccepted) {
		return reviewPaperDAO.selectCountByJournalIdAndIsAccepted(journalId, isAccepted);
	}

	public boolean updateReviewPaperToReleaseComment(String isAccepted, Long reviewPaperId) {
		try {
			ReviewPaper newReviewPaper = reviewPaperDAO.selectById(reviewPaperId);
			newReviewPaper.setIsAccepted(isAccepted);
			reviewPaperDAO.updateReviewPaper(newReviewPaper);

			ReleasedComment releasedComment = new ReleasedComment();

			releasedComment.setPaper(newReviewPaper.getPaper());
			releasedComment.setPerson(newReviewPaper.getPerson());
			releasedComment.setScore1(newReviewPaper.getScore1());
			releasedComment.setScore2(newReviewPaper.getScore2());
			releasedComment.setScore3(newReviewPaper.getScore3());
			releasedComment.setScore4(newReviewPaper.getScore4());
			releasedComment.setScore5(newReviewPaper.getScore5());
			releasedComment.setFinalScore(newReviewPaper.getFinalScore());
			releasedComment.setPaperComment(newReviewPaper.getPaperComment());
			releasedComment.setIsReleased("true");

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			releasedComment.setReleasedDateTime(sdf.format(new Date()));

			releasedCommentDAO.saveReleasedComment(releasedComment);

			return true;
		} catch (Exception e) {
			log.error("updateReviewPaperToReleaseComment(String isAccepted,Long reviewPaperId)-->" + e.getMessage());
		}
		return false;
	}

	public boolean updateToNotUse(ReviewPaper reviewPaper, ReleasedComment releasedComment) {
		try {
			reviewPaperDAO.updateReviewPaper(reviewPaper);
			releasedCommentDAO.deleteReleasedComment(releasedComment.getPid());

			return true;
		} catch (Exception e) {
			log.error("updateToNotUse(ReviewPaper reviewPaper,ReleasedComment releasedComment)" + e.getMessage());
		}
		return false;
	}

	public boolean updateToUsed(ReviewPaper reviewPaper, ReleasedComment releasedComment) {
		try {
			reviewPaperDAO.updateReviewPaper(reviewPaper);

			ReleasedComment selectReleasedComment = releasedCommentDAO.selectByPaperIdAndPersonId(reviewPaper.getPaper().getPid(), reviewPaper.getPerson().getPid());
			if (selectReleasedComment == null) {
				releasedCommentDAO.saveReleasedComment(releasedComment);
			} else {
				selectReleasedComment.setScore1(releasedComment.getScore1());
				selectReleasedComment.setScore2(releasedComment.getScore2());
				selectReleasedComment.setScore3(releasedComment.getScore3());
				selectReleasedComment.setScore4(releasedComment.getScore4());
				selectReleasedComment.setScore5(releasedComment.getScore5());
				selectReleasedComment.setFinalScore(releasedComment.getFinalScore());
				selectReleasedComment.setPaperComment(releasedComment.getPaperComment());
				selectReleasedComment.setPaper(releasedComment.getPaper());
				releasedCommentDAO.updateReleasedComment(selectReleasedComment);
			}

			return true;
		} catch (Exception e) {
			log.error("updateToUsed(ReviewPaper reviewPaper,ReleasedComment releasedComment)-->" + e.getMessage());
		}
		return false;
	}

	public boolean updateToWaitingUse(ReviewPaper reviewPaper, ReleasedComment releasedComment) {
		try {
			reviewPaperDAO.updateReviewPaper(reviewPaper);
			releasedCommentDAO.deleteReleasedComment(releasedComment.getPid());

			return true;
		} catch (Exception e) {
			log.error("updateToWaitingUse(ReviewPaper reviewPaper,ReleasedComment releasedComment)" + e.getMessage());
		}
		return false;
	}

	public List<ReviewPaper> selectByPaperIdAndJournalId(Long paperId, Long journalId, Pagination pagination) {
		return reviewPaperDAO.selectByPaperIdAndJournalId(paperId, journalId, pagination);
	}

	public Long selectCountByJournalIdAndPaperId(Long journalId, Long paperId) {
		return reviewPaperDAO.selectCountByJournalIdAndPaperId(journalId, paperId);
	}

	public Long saveReviewPaper(ReviewPaper reviewPaper) {
		return reviewPaperDAO.saveReviewPaper(reviewPaper);
	}

	public boolean deleteReviewPaper(ReviewPaper reviewPaper) {
		return reviewPaperDAO.deleteReviewPaper(reviewPaper);
	}

	public Long selectCountReviewPaperByIsAccepted(Long personId, String isAccepted) {
		return reviewPaperDAO.selectCountReviewPaperByIsAccepted(personId, isAccepted);
	}

	public List<ReviewPaper> selectByPaperId(Long paperId) {
		return reviewPaperDAO.selectByPaperId(paperId);
	}

	public IReviewPaperDAO getReviewPaperDAO() {
		return reviewPaperDAO;
	}

	public void setReviewPaperDAO(IReviewPaperDAO reviewPaperDAO) {
		this.reviewPaperDAO = reviewPaperDAO;
	}

	public IReleasedCommentDAO getReleasedCommentDAO() {
		return releasedCommentDAO;
	}

	public void setReleasedCommentDAO(IReleasedCommentDAO releasedCommentDAO) {
		this.releasedCommentDAO = releasedCommentDAO;
	}

}
